package com.eastfair.auth.controller;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.auth.dto.*;
import com.eastfair.auth.entity.Organization;
import com.eastfair.auth.entity.Role;
import com.eastfair.auth.entity.UserAccount;
import com.eastfair.auth.exceptioncode.AuthServiceExceptionCode;
import com.eastfair.auth.service.*;
import com.eastfair.auth.util.DataIsoLotionUtil;
import com.eastfair.cache.lock.DistributedLock;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.security.handler.DataIsolactionHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 资源-场馆内部组织
 *
 * </p>
 *
 * @author ligang
 * @date 2022-05-16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/iam")
@Api(value = "HuaWeiIamController", tags = "资源-场馆内部组织")
public class HuaWeiIamController {

    @Autowired
    DataIsolactionHandler dataIsolactionHandler;


    @Autowired
    UserAccountService userAccountService;

    @Autowired
    OrganizationService organizationService;

    @Autowired
    RoleService roleService;

    @Value("${com.eastfair.projectId}")
    private Long commonProjectId;

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    private DistributedLock distributedLock;

    /**
     * 基础接口
     *
     * @param huaWeiIamDTO 实体
     * @return
     */
    @ApiOperation(value = "基础接口", notes = "基础接口")
    @PostMapping("/{systemscene}/SchemaService")
    @SysLog("基础接口")
    public JSONObject SchemaService(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiIamDTO huaWeiIamDTO) {
        log.info("基础接口 is execute ");
        log.info(JSON.toJSONString(huaWeiIamDTO));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("iamRequestId", huaWeiIamDTO.getIamRequestId());

        HuaWeiSchemaDTO huaWeiSchemaDTO = new HuaWeiSchemaDTO();

        List<HuaWeiSchemaDTO> userSchemaDTOList = new ArrayList<>();
        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("uid");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);


        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("loginName");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);


        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("orgId");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);


        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("fullName");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);

        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("mobile");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);



        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("status");
        huaWeiSchemaDTO.setType("int");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);

        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("roleId");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(true);
        huaWeiSchemaDTO.setRequired(false);
        userSchemaDTOList.add(huaWeiSchemaDTO);


        jsonObject.put("account", userSchemaDTOList);


        userSchemaDTOList = new ArrayList<>();
        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("orgId");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);

        userSchemaDTOList = new ArrayList<>();
        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("huaweiOrgId");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);


        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("name");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);

        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("parentId");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);

        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("status");
        huaWeiSchemaDTO.setType("int");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);

        jsonObject.put("organization", userSchemaDTOList);

        userSchemaDTOList = new ArrayList<>();


        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("roleId");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);

        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("roleName");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);


        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("code");
        huaWeiSchemaDTO.setType("String");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);


        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("isDelete");
        huaWeiSchemaDTO.setType("int");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);

        huaWeiSchemaDTO = new HuaWeiSchemaDTO();
        huaWeiSchemaDTO.setName("isNoAction");
        huaWeiSchemaDTO.setType("int");
        huaWeiSchemaDTO.setMultivalued(false);
        huaWeiSchemaDTO.setRequired(true);
        userSchemaDTOList.add(huaWeiSchemaDTO);


        jsonObject.put("role", userSchemaDTOList);
//        return R.success(jsonObject);
        return jsonObject;
    }


    /**
     * 创建用户
     *
     * @param huaWeiAccountDTO 实体
     * @return
     */
    @ApiOperation(value = "创建用户", notes = "创建用户")
    @PostMapping("/{systemscene}/UserCreateService")
    @SysLog("创建用户")
    public JSONObject userCreate(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiAccountDTO huaWeiAccountDTO) {
        String lockKey = huaWeiAccountDTO.getLoginName();
        if (distributedLock.lock(lockKey,2000)) {
            try {
                log.info("创建用户 is execute ");
                log.info(systemscene);
                log.info(JSON.toJSONString(huaWeiAccountDTO));
                //{"fullName":"ab1","iamRemotePwd":"password","iamRemoteUser":"IAMadmin","iamRequestId":"26e134626404481dada1c68cb7ac6aab","loginName":"ab1","orgId":"20221220112338783-132C-8F8E384FB","status":0}
                ContextUtil.setProjectId(commonProjectId);
                log.info("projectid is {} ", commonProjectId);
                ContextUtil.setSystemScene(DataIsoLotionUtil.parseSystemscene(systemscene));
                log.info("systemScene is {} ", ContextUtil.getSystemScene());
                ContextUtil.setSubSystemId(dataIsolactionHandler.assmableSystemId(ContextUtil.getSystemScene()));
                log.info("SubSystemId is {} ", ContextUtil.getSubSystemId());
                if (CollectionUtil.isNotEmpty(huaWeiAccountDTO.getRoleId())) {
                    //判断角色
                    List<Long> roleIds = new ArrayList<>();
                    for (String id : huaWeiAccountDTO.getRoleId()) {
                        roleIds.add(Long.parseLong(id));
                    }
                    List<Role> roleList = roleService.list(null, null, roleIds);
                    if (CollectionUtil.isNotEmpty(roleList)) {
                        if (roleList.size() != huaWeiAccountDTO.getRoleId().size()) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                            jsonObject.put("resultCode", "1");
                            jsonObject.put("message", "角色不可用");
                            return jsonObject;
                        }
                    }
                }

                UserAccountDto userAccountDto = new UserAccountDto();
                userAccountDto.setName(huaWeiAccountDTO.getLoginName());
                userAccountDto.setMobile(huaWeiAccountDTO.getMobile());
                List<UserAccount> userAccountList = userAccountService.queryUserOfregister(userAccountDto);
                if(CollectionUtil.isNotEmpty(userAccountList) && userAccountList.size()>1){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                    jsonObject.put("resultCode", "1");
                    jsonObject.put("message", "手机号和登录名匹配到多个账号，需要检查参数是否正确");
                    return jsonObject;
                }


                HuaWeiAccountDTO huaWeiAccountDTO1 = userAccountService.sycnAcctionOfCreate(huaWeiAccountDTO);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                if (huaWeiAccountDTO1 == null) {
                    jsonObject.put("resultCode", "1");
                    jsonObject.put("message", "所属机构不存在");
                } else {
                    jsonObject.put("resultCode", "0");
                    jsonObject.put("message", "success");
                    jsonObject.put("uid", huaWeiAccountDTO1.getUid());
                }
                return jsonObject;
            } finally {
                try {
                    distributedLock.releaseLock(lockKey);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        } else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
            jsonObject.put("resultCode", "1");
            jsonObject.put("message", "请求超时");
            return jsonObject;
        }
    }


    /**
     * 更新用户
     *
     * @param huaWeiAccountDTO 实体
     * @return
     */
    @ApiOperation(value = "更新用户", notes = "更新用户")
    @PostMapping("/{systemscene}/UserUpdateService")
    @SysLog("更新用户")
    public JSONObject userUpd(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiAccountDTO huaWeiAccountDTO) {
        log.info("UserUpdateService 更新用户 is execute ");
        log.info(systemscene);
        log.info(JSON.toJSONString(huaWeiAccountDTO));
        ContextUtil.setProjectId(commonProjectId);
        ContextUtil.setSystemScene(DataIsoLotionUtil.parseSystemscene(systemscene));
        ContextUtil.setSubSystemId(dataIsolactionHandler.assmableSystemId(ContextUtil.getSystemScene()));
        log.info("UserUpdateService 判断角色是否可用");
        if (CollectionUtil.isNotEmpty(huaWeiAccountDTO.getRoleId())) {
            //判断角色
            List<Long> roleIds = new ArrayList<>();
            for (String id : huaWeiAccountDTO.getRoleId()) {
                roleIds.add(Long.parseLong(id));
            }
            boolean pan = false;
            List<Role> roleList = roleService.list(null, null, roleIds);
            if (CollectionUtil.isNotEmpty(roleList)) {
                if (roleList.size() != huaWeiAccountDTO.getRoleId().size()) {
                    pan = true;
                }
            } else {
                pan = true;
            }
            if (pan) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                jsonObject.put("resultCode", "1");
                jsonObject.put("message", "角色不可用");
                return jsonObject;
            }
        }
        //判断用户是否在该系统里
        log.info("UserUpdateService 判断用户是否在该系统里");
        UserAccount userAccount = userAccountService.getByIdCache(Long.parseLong(huaWeiAccountDTO.getUid()));
        if (userAccount != null) {
            //用户是否在所在系统
            log.info("用户是否在系统里 is{}", userAccountService.isSubSystem(userAccount));
            if (BusinessConstant.YES != userAccountService.isSubSystem(userAccount)) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                jsonObject.put("resultCode", "1");
                jsonObject.put("message", "用户不存在");
                return jsonObject;
            }
        } else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
            jsonObject.put("resultCode", "1");
            jsonObject.put("message", "用户不存在");
            return jsonObject;
        }

        UserAccountDto userAccountDto = new UserAccountDto();
        userAccountDto.setMobile(huaWeiAccountDTO.getMobile());
        List<UserAccount> userAccountList = userAccountService.queryUserOfregister(userAccountDto);
        if(CollectionUtil.isNotEmpty(userAccountList) ){
            Boolean pan= false;
            if(userAccountList.size()>1){
                pan= true;
            }else  if(userAccountList.size()==1 && !userAccountList.get(0).getId().equals(Long.parseLong(huaWeiAccountDTO.getUid()))){
                pan= true;
            }
            if(pan){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                jsonObject.put("resultCode", "1");
                jsonObject.put("message", "手机号和登录名匹配到多个账号，需要检查参数是否正确");
                return jsonObject;
            }

        }


        //清空角色
        log.info("UserUpdateService 清空角色");
        userRoleService.removeUserRole(Arrays.asList(Long.parseLong(huaWeiAccountDTO.getUid())), null);
        log.info("UserUpdateService 保存华为推送新数据");
        HuaWeiAccountDTO huaWeiAccountDTO1 = userAccountService.sycnAcctionOfUpd(huaWeiAccountDTO);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
        jsonObject.put("resultCode", "0");
        jsonObject.put("message", "success");
        return jsonObject;
    }

    /**
     * 删除用户
     *
     * @param huaWeiAccountDTO 实体
     * @return
     */
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @PostMapping("/{systemscene}/UserDeleteService")
    @SysLog("删除用户")
    public JSONObject userDel(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiAccountDTO huaWeiAccountDTO) {
        String lockKey = huaWeiAccountDTO.getUid();
        if (distributedLock.lock(lockKey,2000)) {
            try {
                log.info("删除用户 is execute ");
                log.info(systemscene);
                log.info(JSON.toJSONString(huaWeiAccountDTO));
                ContextUtil.setProjectId(commonProjectId);
                ContextUtil.setSystemScene(DataIsoLotionUtil.parseSystemscene(systemscene));
                ContextUtil.setSubSystemId(dataIsolactionHandler.assmableSystemId(ContextUtil.getSystemScene()));
                //判断用户是否在该系统里
                UserAccount userAccount = userAccountService.getByIdCache(Long.parseLong(huaWeiAccountDTO.getUid()));
                if (userAccount != null) {
                    log.info("用户是否在系统里 is{}", userAccountService.isSubSystem(userAccount));
                    //用户是否在所在系统
                    if (BusinessConstant.YES != userAccountService.isSubSystem(userAccount)) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                        jsonObject.put("resultCode", "1");
                        jsonObject.put("message", "用户不存在");
                        return jsonObject;
                    }
                } else {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                    jsonObject.put("resultCode", "1");
                    jsonObject.put("message", "用户不存在");
                    return jsonObject;
                }
                HuaWeiAccountDTO huaWeiAccountDTO1 = userAccountService.sycnAcctionOfDel(huaWeiAccountDTO);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
                jsonObject.put("resultCode", "0");
                jsonObject.put("message", "success");
                return jsonObject;
            } finally {
                try {
                    distributedLock.releaseLock(lockKey);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        } else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("iamRequestId", huaWeiAccountDTO.getIamRequestId());
            jsonObject.put("resultCode", "1");
            jsonObject.put("message", "请求超时");
            return jsonObject;
        }

    }


    /**
     * 创建组织
     *
     * @param huaWeiOrganizationDTO 实体
     * @return
     */
    @ApiOperation(value = "创建组织", notes = "创建组织")
    @PostMapping("/{systemscene}/OrgCreateService")
    @SysLog("创建组织")
    public JSONObject OrgCreateService(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiOrganizationDTO huaWeiOrganizationDTO) {
        log.info("创建组织 is execute ");
        log.info(systemscene);
        log.info(JSON.toJSONString(huaWeiOrganizationDTO));
        ContextUtil.setProjectId(commonProjectId);
        ContextUtil.setSystemScene(DataIsoLotionUtil.parseSystemscene(systemscene));
        ContextUtil.setSubSystemId(dataIsolactionHandler.assmableSystemId(ContextUtil.getSystemScene()));
        HuaWeiOrganizationDTO huaWeiOrganizationDTO1 = organizationService.sycnOrgOfCreate(huaWeiOrganizationDTO);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("iamRequestId", huaWeiOrganizationDTO.getIamRequestId());
        jsonObject.put("orgId", huaWeiOrganizationDTO1.getOrgId());
        jsonObject.put("resultCode", "0");
        jsonObject.put("message", "success");
        return jsonObject;
    }


    /**
     * 修改组织
     *
     * @param huaWeiOrganizationDTO 实体
     * @return
     */
    @ApiOperation(value = "修改组织", notes = "修改组织")
    @PostMapping("/{systemscene}/OrgUpdateService")
    @SysLog("修改组织")
    public JSONObject OrgUpdateService(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiOrganizationDTO huaWeiOrganizationDTO) {
        log.info("修改组织 is execute ");
        log.info(systemscene);
        log.info(JSON.toJSONString(huaWeiOrganizationDTO));
        ContextUtil.setProjectId(commonProjectId);
        ContextUtil.setSystemScene(DataIsoLotionUtil.parseSystemscene(systemscene));
        ContextUtil.setSubSystemId(dataIsolactionHandler.assmableSystemId(ContextUtil.getSystemScene()));
        HuaWeiOrganizationDTO huaWeiOrganizationDTO1 = organizationService.sycnOrgOfUpd(huaWeiOrganizationDTO);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("iamRequestId", huaWeiOrganizationDTO.getIamRequestId());
        jsonObject.put("resultCode", "0");
        jsonObject.put("message", "success");
        return jsonObject;
    }


    /**
     * 删除组织
     *
     * @param huaWeiOrganizationDTO 实体
     * @return
     */
    @ApiOperation(value = "删除组织", notes = "删除组织")
    @PostMapping("/{systemscene}/OrgDeleteService")
    @SysLog("删除组织")
    public JSONObject OrgDeleteService(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiOrganizationDTO huaWeiOrganizationDTO) {
        log.info("删除组织 is execute ");
        log.info(systemscene);
        log.info(JSON.toJSONString(huaWeiOrganizationDTO));
        ContextUtil.setProjectId(commonProjectId);
        ContextUtil.setSystemScene(DataIsoLotionUtil.parseSystemscene(systemscene));
        ContextUtil.setSubSystemId(dataIsolactionHandler.assmableSystemId(ContextUtil.getSystemScene()));
        HuaWeiOrganizationDTO huaWeiOrganizationDTO1 = organizationService.sycnOrgOfDel(huaWeiOrganizationDTO);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("iamRequestId", huaWeiOrganizationDTO.getIamRequestId());
        jsonObject.put("resultCode", "0");
        jsonObject.put("message", "success");
        return jsonObject;
    }


    /**
     * 角色批量查询
     *
     * @param huaWeiOrganizationDTO 实体
     * @return
     */
    @ApiOperation(value = "角色批量查询", notes = "角色批量查询")
    @PostMapping("/{systemscene}/FindAllRoleIdsService")
    @SysLog("角色批量查询")
    public JSONObject FindAllRoleIdsService(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiOrganizationDTO huaWeiOrganizationDTO) {
        log.info("FindAllRoleIdsService is execute ");
        log.info(systemscene);
        log.info(JSON.toJSONString(huaWeiOrganizationDTO));
        ContextUtil.setProjectId(commonProjectId);
        ContextUtil.setSystemScene(DataIsoLotionUtil.parseSystemscene(systemscene));
        ContextUtil.setSubSystemId(dataIsolactionHandler.assmableSystemId(ContextUtil.getSystemScene()));
        List<Role> roleList = roleService.list();
        log.info("roleList is {}", JSONObject.toJSONString(roleList));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("iamRequestId", huaWeiOrganizationDTO.getIamRequestId());
        jsonObject.put("resultCode", "0");
        jsonObject.put("message", "success");
        if (CollectionUtil.isNotEmpty(roleList)) {
            jsonObject.put("roleIdList", roleList.stream().map(Role::getId).collect(Collectors.toList()));
        } else {
            jsonObject.put("roleIdList", null);
        }
        log.info("jsonObject is {}", jsonObject);
        return jsonObject;
    }

    /**
     * 单个角色详情查询
     *
     * @param huaWeiOrganizationDTO 实体
     * @return
     */
    @ApiOperation(value = "单个角色详情查询", notes = "单个角色详情查询")
    @PostMapping("/{systemscene}/FindRoleByIdService")
    @SysLog("单个角色详情查询")
    public JSONObject FindRoleByIdService(@PathVariable("systemscene") String systemscene, @RequestBody HuaWeiOrganizationDTO huaWeiOrganizationDTO) {
        log.info("删除组织 is execute ");
        log.info(systemscene);
        log.info(JSON.toJSONString(huaWeiOrganizationDTO));
        ContextUtil.setProjectId(commonProjectId);
        ContextUtil.setSystemScene(DataIsoLotionUtil.parseSystemscene(systemscene));
        ContextUtil.setSubSystemId(dataIsolactionHandler.assmableSystemId(ContextUtil.getSystemScene()));
        Role role = roleService.getByIdCache(huaWeiOrganizationDTO.getRoleId());
        log.info("role detail is {}", JSONObject.toJSONString(role));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("iamRequestId", huaWeiOrganizationDTO.getIamRequestId());
        jsonObject.put("resultCode", "0");
        jsonObject.put("message", "success");
        JSONObject jsonObject1 = new JSONObject();
        if (role != null) {
            jsonObject1.put("roleId", role.getId());
            jsonObject1.put("roleName", role.getName());
            jsonObject1.put("updateTime", huaWeiOrganizationDTO.getUpdateTime());
            jsonObject1.put("code", role.getCode());
            jsonObject1.put("isDelete", role.getIsDeleted());
            jsonObject1.put("isNoAction", BusinessConstant.YES == role.getIsEnabled() ? 0 : 1);
            jsonObject.put("role", jsonObject1);
        } else {
            jsonObject1.put("roleId", huaWeiOrganizationDTO.getRoleId());
//            jsonObject1.put("roleName",role.getName());
            jsonObject1.put("updateTime", huaWeiOrganizationDTO.getUpdateTime());
//            jsonObject1.put("code",role.getCode());
            jsonObject1.put("isDelete", 1);
//            jsonObject1.put("isDisabled", BusinessConstant.YES == role.getIsEnabled()?0:1);
            jsonObject.put("role", jsonObject1);
        }
        log.info("FindRoleByIdService jsonObject is {}", jsonObject);


        return jsonObject;
    }


    /**
     * 查看用户是否有综合权限
     *
     * @param loginName 账号
     * @return
     */
    @ApiOperation(value = "", notes = "查看用户是否有综合权限")
    @GetMapping("/isUserRoleOfCompre")
    @SysLog("查看用户是否有综合权限")
    public R<Boolean> isUserRoleOfCompre(String loginName) {
        log.info("查看用户是否有综合权限 is execute ");
        ContextUtil.setProjectId(commonProjectId);
        UserAccountDto userAccountDto2 = new UserAccountDto();
        userAccountDto2.setName(loginName);
        List<UserAccount> userAccountList = userAccountService.getUserByNameAllDb(userAccountDto2);
        if (CollectionUtil.isNotEmpty(userAccountList)) {
            if (userAccountList.get(0).getIsPCompositeVenue() != null && BusinessConstant.YES == userAccountList.get(0).getIsPCompositeVenue()) {
                return R.success(true);
            }
        }
        return R.success(false);
    }


}
